﻿using Microsoft.AspNetCore.Mvc;

using ManYan.Application.WechatUser;
using ManYan.Application.WechatUser.Dto;
using Microsoft.AspNetCore.Authorization;
using System.Security.Claims;
using Microsoft.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.Text;
using ManYan.Common;
using Newtonsoft.Json.Linq;
using ManYan.Common.jwt;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion.Internal;
using System.Text.Json.Nodes;
using ManYan.Domain.Model.Entity;
using Microsoft.EntityFrameworkCore;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace ManYan.Api.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class WechatUserController : ControllerBase
    {
        private readonly IWechatUserAppService _userService;
        private readonly IHttpContextAccessor _httpContextAccessor;
        public WechatUserController(IWechatUserAppService userService, IHttpContextAccessor httpContextAccessor)
        {
            _userService = userService;
            this._httpContextAccessor = httpContextAccessor;
        }

        /// <summary>
        /// 微信小程序登录时授权登录信息同步（授权方法）
        /// </summary>
        /// <param name="dto"></param>
        // POST api/<WechatUserController>
        [HttpPost]
        [AllowAnonymous]
        public async Task<Res> Post([FromBody] WechatUserAddDto dto)
        {

            return await _userService.WechatLoginAsync(dto);
        }
        [HttpGet]
        public Res<WechatUser> GetUserList()
        {

            //如果你是系统管理员，查询所有的数据
            //如果你部门管理员，只查本部门数据
            return new Res<WechatUser>() { };
        }
        /// <summary>
        /// 系统登录（匿名方法）
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpPost]

        public Task<Res> WechatLogin([FromForm] WechatUserAddDto dto)
        {
        
            var header = _httpContextAccessor.HttpContext?.Request.Headers;

            dto.IP = _httpContextAccessor?.HttpContext?.Connection.RemoteIpAddress + string.Empty;
            return _userService.WechatLoginAsync(dto);


        }
        


    }
}
